package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Reference
    private OrderService orderService;

    @RequestMapping("/findPage")
    public Result findPage(@RequestBody Map map){
        try {
            PageResult pageResult = orderService.pageQuery(map);
            return new Result(true,null,pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/add")
    public Result add(@RequestBody Map map,Integer setmealIds){
        try {
            orderService.add(map,setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        return new Result(true,MessageConstant.ORDER_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            orderService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_ORDER_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_ORDER_SUCCESS);
    }

    @RequestMapping("/changeOrderStatus")
    public Result changeOrderStatus(Integer orderId){
        try {
            orderService.changeOrderStatus(orderId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.CHANGE_ORDERSTATUS_FAIL);
        }
        return new Result(true,MessageConstant.CHANGE_ORDERSTATUS_SUCCESS);
    }


    @RequestMapping("/findOrderById")
    public Result findOrderById(Integer orderId){
        try{
            Map<String,Object> orders = orderService.findOrderById(orderId);
            return new Result(true,MessageConstant.QUERY_ORDER_SUCCESS,orders);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    @RequestMapping("/edit")
    public Result edit(Map map , Integer setmealIds){
        try{
            orderService.edit(map,setmealIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ORDER_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_ORDER_SUCCESS);
    }
}
